<template>
  <div class="home-category" @mouseleave='infoId=null'>
    <ul class="menu">
      <li v-for="item in list" :key="item.id" @mouseenter="infoId = item.id" :class="{'active':infoId === item.id}">
        <RouterLink :to="`/category/${item.id}`">{{ item.name }}</RouterLink>
        <template  v-for="val in item.children" :key="val.id">
          <RouterLink :to="`/category/sub/${val.id}`">{{ val.name }}</RouterLink>
        </template>
        <!-- 骨架屏 -->
       <span v-if="!item.children">
         <XtxSkeleton width="60px" height="18px" style="margin-right:5px" bg="rgba(255,255,255,0.2)" />
         <XtxSkeleton width="50px" height="18px" bg="rgba(255,255,255,0.2)" />
       </span>
      </li>
    </ul>
    <!-- 弹层 -->
    <div class="layer" v-if="infoList">
      <h4>{{infoList.goods?'分类':'品牌'}}推荐 <small>根据您的购买或浏览记录推荐</small></h4>
      <!-- 分类 -->
      <ul v-if="infoList&&infoList.goods">
        <li v-for="item in infoList.goods" :key="item.id">
          <RouterLink :to="`/product/${item.id}`">
            <img :src="item.picture" alt="" />
            <div class="info">
              <p class="name ellipsis-2">{{ item.name }}</p>
              <p class="price"><i>¥</i>{{ item.price }}</p>
            </div>
          </RouterLink>
        </li>
      </ul>
      <!-- 品牌 -->
      <ul v-if="infoList&&infoList.brands">
        <li class="brand" v-for="i in infoList.brands" :key="i.id">
          <RouterLink to="/">
            <img :src="i.picture" alt=""/>
            <div class="info">
              <p class="place"><i class="iconfont icon-dingwei"></i>{{i.place}}</p>
              <p class="name ellipsis">{{i.name}}</p>
              <p class="desc ellipsis-2">{{i.desc}}</p>
            </div>
          </RouterLink>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { computed, ref, reactive } from 'vue'
import { useStore } from 'vuex'
import { findBrand } from '@/api/category'
export default {
  name: 'HomeCategory',
  setup () {
    const brand = reactive({
      id: 9999,
      name: '品牌',
      children: [{ id: 10086, name: '品牌推荐' }]
    })
    // 获取首页左侧菜单品牌的数据
    findBrand(6).then((res) => {
      brand.brands = res.result
    })
    const store = useStore()
    // 用于左侧渲染的数组
    const list = computed(() => {
      const arr = store.state.category.list.map((v) => {
        return {
          ...v,
          // 只提取二级分类的前两项显示
          // 🔔 v.children && 写法，防止初始化还没有 children 的时候调用 slice 函数报错
          children: v.children && v.children.slice(0, 2)
        }
      })
      // 追加品牌推荐，类别只有9个，但是左侧菜单有 10 个，最后一个需自己添加上去
      arr.push(brand)
      return arr
    })
    // 右侧信息
    const infoId = ref(0)
    const infoList = computed(() => {
      return list.value.find((v) => infoId.value === v.id)
    })
    return { list, infoId, infoList }
  }
}
</script>

<style scoped lang="less">
.xtx-skeleton {
  animation: fade 1s linear infinite alternate;
}
@keyframes fade {
  from {
    opacity: 0.2;
  }
  to {
    opacity: 1;
  }
}
.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  z-index: 99;
  // 左侧菜单
  .menu {
    li {
      padding-left: 40px;
      height: 50px;
      line-height: 50px;
      &.active {
        background: @xtxColor;
      }
      a {
        margin-right: 4px;
        color: #fff;
        &:first-child {
          font-size: 16px;
        }
      }
    }
  }
  // 左侧菜单对应的弹出层
  .layer {
    width: 990px;
    height: 500px;
    background: rgba(255, 255, 255, 0.8);
    position: absolute;
    left: 250px;
    top: 0;
    display: none;
    padding: 0 15px;
    h4 {
      font-size: 20px;
      font-weight: normal;
      line-height: 80px;
      small {
        font-size: 16px;
        color: #666;
      }
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        width: 310px;
        height: 120px;
        margin-right: 15px;
        margin-bottom: 15px;
        border: 1px solid #eee;
        border-radius: 4px;
        background: #fff;
        &:nth-child(3n) {
          margin-right: 0;
        }
        a {
          display: flex;
          width: 100%;
          height: 100%;
          align-items: center;
          padding: 10px;
          &:hover {
            background: #e3f9f4;
          }
          img {
            width: 95px;
            height: 95px;
          }
          .info {
            padding-left: 10px;
            line-height: 24px;
            width: 190px;
            .name {
              font-size: 16px;
              color: #666;
            }
            .desc {
              color: #999;
            }
            .price {
              font-size: 22px;
              color: @priceColor;
              i {
                font-size: 16px;
              }
            }
          }
        }
      }
      li.brand {
        height: 180px;
        a {
          align-items: flex-start;
          img {
            width: 120px;
            height: 160px;
          }
          .info {
            p {
              margin-top: 8px;
            }
            .place {
              color: #999;
            }
          }
        }
      }
    }
  }
  &:hover {
    .layer {
      display: block;
    }
  }
}
</style>
